<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;


use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;

class Ad extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','ad');
        $this->assign('allposition',['1'=>'首页顶部广告(980*80)','2'=>'首页底部广告(980*80)','3'=>'所有页面顶部广告(980*80)','4'=>'所有页面底部广告(980*80)','5'=>'首页轮播图(730*300)']);
    }
    function index()
    {
        //获取数据库操作对象
        $db = new BaseDao();
        //获取全部友情链接，并按ord排序
        $data = $db->select('ad','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        //将数据分配给模板
        $this->assign('data',$data);
        //标题
        $this->assign('title','广告列表');
        //输出模板
        $this->display('ad/index');
    }
    function add()
    {
        if($_POST['do_submit']){
            $path = TEMPDIR."/uploads/ad";

            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder

                //add constraints

                ->allowExtensions(['jpg', 'png', 'gif'])
                ->allowMimeTypes(['image/*'])

                ->saveTo($path) //save to local
                ->getHandler();
            $files = $handler->handle();
            $filename = $files['logo']->getUploadedFile()->getClientOriginalName();

            $newfilename =  date('Y-md') . '-' . uniqid() . '.' . $files['logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename, $path.'/'.$newfilename);

            $db = new BaseDao();

            unset($_POST['do_submit']);

            $_POST['logo'] = $newfilename;

            if($db->insert('ad',$_POST)){
                $this->success('/admin/ad','添加成功！');
            }else{
                $this->error('/admin/ad','添加失败！');
            }
        }
        $this->assign('title','广告添加');
        $this->display('ad/add');
    }
    function mod($id)
    {
        $db = new BaseDao();
        $data = $db->get('ad','*',['id'=>$id]);
        $this->assign('ad',$data);
        $this->assign('title','修改广告');
        $this->display('ad/mod');
    }
    function doupdate()
    {
        $id = $_POST['id'];
        unset($_POST['id']);

        if($_FILES['logo']['error']==0) {
            $path = TEMPDIR."/uploads/ad";

            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder

                //add constraints

                ->allowExtensions(['jpg', 'png', 'gif'])
                ->allowMimeTypes(['image/*'])

                ->saveTo($path) //save to local
                ->getHandler();


            $files = $handler->handle();
            $filename = $files['logo']->getUploadedFile()->getClientOriginalName();

            $newfilename =  date('Y-md') . '-' . uniqid() . '.' . $files['logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename, $path.'/'.$newfilename);

            $_POST['logo'] = $newfilename;
        }


        $db = new BaseDao();
        if($db->update('ad',$_POST,['id'=>$id])){
            $this->success('/admin/ad','修改成功！');
        }else{
            $this->error('/admin/ad/mod/'.$id,'修改失败！');
        }
    }
    function del($id)
    {
        $db = new BaseDao();
        $logo = $db->get('ad', 'logo', ['id'=>$id]);

        if($db->delete('ad', ['id'=>$id])) {
            $path = TEMPDIR."/uploads/ad";

            @unlink($path.'/'.$logo);


            $this->success('/admin/ad', '删除成功！');
        }else{
            $this->error('/admin/ad', '删除失败！');
        }
    }
    function order()
    {
        $db = new BaseDao();
        $num = 0;
        foreach ($_POST['ord'] as $id=>$ord){
            $num += $db->update('ad',['ord'=>$ord],['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/ad','重新排序成功！');
        }else{
            $this->error('/admin/ad','重新排序失败！');
        }
    }
}